/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jsysacc.asistencia.model.dao.component.maestros;

import com.cerd.util.BaseSP01.JUtilBaseSP01;
import com.jsysacc.asistencia.model.beans.maestros.Permiso;
import com.jsysacc.asistencia.model.dao.design.maestros.IPermisoDAO;
import com.jsysacc.asistencia.model.dao.ds.Cnx;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import org.joda.time.DateTime;

/**
 *
 * @author Carlos
 */
public class PermisoDAO extends JUtilBaseSP01 implements IPermisoDAO {

    private Connection cnx = null;
    private PreparedStatement pst = null;
    private Statement stm = null;
    private ResultSet rs = null;
    private CallableStatement cs = null;

    @Override
    public boolean insert(Permiso entity) throws SQLException {
        String sql = " { ? = CALL " + Cnx.getSchema() + ".sp_t_permisos_insert(  ?, ? , ? ,?, ?, ? ) } ";
        cnx = Cnx.getConnection();
        cnx.setAutoCommit(false);
        try {
            cs = cnx.prepareCall(sql);
            cs.registerOutParameter(1, Types.BOOLEAN);
            cs.setString(2, entity.getCodigo());
            cs.setString(3, entity.getDescrip());
            cs.setInt(4, entity.getNivel());
            cs.setString(5, "A");
            cs.setString(6, entity.getUsu_reg());
            cs.setTimestamp(7, toTimestampSQL(DateTime.now()));
            cs.execute();
            if (!cs.getBoolean(1)) {
                throw new SQLException("ERROR AL INSERTAR EL REGISTRO");
            }
            cnx.commit();
            return true;
        } catch (SQLException ex) {
            cnx.rollback();
            throw new SQLException(ex.getMessage());
        } finally {
            try {
                if (cs != null) {
                    cs.close();
                }
                if (cnx != null) {
                    cnx.close();
                }
            } catch (SQLException ex) {
                throw new SQLException(ex.getMessage());
            }
        }
    }

    @Override
    public boolean deleteAll() throws SQLException {
        String sql = " { ? = CALL " + Cnx.getSchema() + ".sp_t_permisos_delete_all() } ";
        cnx = Cnx.getConnection(); //sp_t_permisos_delete_all
        cnx.setAutoCommit(false);
        try {
            cs = cnx.prepareCall(sql);
            cs.registerOutParameter(1, Types.BOOLEAN);
            cs.execute();
            if (!cs.getBoolean(1)) {
                throw new SQLException("ERROR AL ELIMINAR EL REGISTRO");
            }
            cnx.commit();
            return true;
        } catch (SQLException ex) {
            cnx.rollback();
            throw new SQLException(ex.getMessage());
        } finally {
            try {
                if (cs != null) {
                    cs.close();
                }
                if (cnx != null) {
                    cnx.close();
                }
            } catch (SQLException ex) {
                throw new SQLException(ex.getMessage());
            }
        }
    }

}
